#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "GATetris.h"

#define MIN(_a, _b) ( (_a) > (_b)? (_b) : (_a))
#define MAX(_a, _b) ( (_a) > (_b)? (_a) : (_b))

void GAShape::TurnRight()
{
	int index;
	for(index = 0; index < 4; index++)
	{
		int x = m_ptCoord[index].x;
		int y = m_ptCoord[index].y;
		m_ptCoord[index].x = -y;
		m_ptCoord[index].y = x;
	}

}

void GAShape::TurnLeft()
{
	int index;
	for(index = 0; index < 4; index++)
	{
		int x = m_ptCoord[index].x;
		int y = m_ptCoord[index].y;
		m_ptCoord[index].x = y;
		m_ptCoord[index].y = -x;
	}
}

int GAShape::LeftBnd()
{
	int index; 
	int x = m_ptCoord[0].x;
	for(index = 1; index < 4; index++)
	{
		x = MIN(x, m_ptCoord[index].x);
	}
	return x;

}
int GAShape::RightBnd()
{
	int index; 
	int x = m_ptCoord[0].x;
	for(index = 1; index < 4; index++)
	{
		x = MAX(x, m_ptCoord[index].x);
	}
	return x;


}
int GAShape::TopBnd()
{
	int index;
	int y = m_ptCoord[0].y;
	for(index = 1; index < 4; index++)
	{
		y = MIN(y, m_ptCoord[index].y);

	}
	return y;
}
int GAShape::BottomBnd()
{
	int index;
	int y = m_ptCoord[0].y;
	for(index = 1; index < 4; index++)
	{
		y = MAX(y, m_ptCoord[index].y);

	}
	return y;

}



GAShape::GAShape(SHAPE_TYPE nShapeType, int nRed, int nGreen, int nBlue)
{
	TETRIS_POINT ppCoord[TYPE_COUNT][4] = 
	{
		{ {0, 0}, {0, 0}, {0, 0}, {0,0} }, //NULL
		{ {0, 0}, {0, 1}, {0, 2}, {1, 2} }, //L
		{ {0, 0}, {1, 0}, {1, 1}, {2, 1} }, //Z
		{ {0, 0}, {1, 0}, {1, 1}, {0, 1} }, //square
		{ {0, 0}, {0, 1}, {0, 2}, {0, 3} }, //I
		{ {0, 0}, {1, 0}, {2, 0}, {1, 1} }, //T
		{ {0, 0}, {0, 1}, {0, 2}, {-1, 2} }, //Flip L
		{ {1, 0}, {0, 0}, {0, 1}, {-1, 1} }, //S
	};


	int index;
	for(index = 0; index < 4; index++)
	{
		m_ptCoord[index].x = ppCoord[(int)nShapeType][index].x;
		m_ptCoord[index].y = ppCoord[(int)nShapeType][index].y;
	}
	m_nShapeType = nShapeType;

	m_nColor[0] = nRed;
	m_nColor[1] = nGreen;
	m_nColor[2] = nBlue;

}

GAShape::~GAShape()
{
	m_nShapeType = SHAPE_NULL;

}


